package cn.com.gridinfo.sooc.core.login.activity.forgetpwd;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.StringUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.core.login.dao.ForgetDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 忘记密码，首页--
 * By  刘晓晨
 */
public class ForgetPwdActivity extends BaseSoocActivity {


    @InjectView(R.id.btn_next)
    Button btnNext;//“下一步”按钮
    ForgetDao dao;// 忘记密码dao
    @InjectView(R.id.account)
    ClearEditText account;//账号输入框
    private String userAccount;//全局变量，账号
    List<NodeServerInfo> list;
    NodeServerInfo nodeServerInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forget_pwd_activity);
        SoocApplication.getContext().addActivity(this);
        ButterKnife.inject(this);
        dao = new ForgetDao(this);
        btnNext.setEnabled(false);//按钮初始设置不可点击
        if (!StringUtils.isNull(getIntent().getStringExtra("account"))) {
            account.setText(getIntent().getStringExtra("account"));
            btnNext.setEnabled(true);//填充账号后按钮可点击
        }
        account.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        account.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    if (validateNotNull()) {
                        setNext();
                    }

                }
                return false;
            }
        });
    }

    private boolean validateNotNull() {
        if (StringUtils.isNull(account.getText().toString().trim())) {
            account.requestFocus();
            return false;
        }
        return true;
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "忘记密码";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();
    }


    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        switch (requestCode) {
            case CodeConst.GET_MYSCHOOLLIST://获得注册过的节点
                if (dao.count == 0) {//节点数量为0
                    MessageUtils.showImageShortToast(this, "此账号不存在");
                } else if (dao.count == 1) {
                    //只有一个节点，直接跳找回方式
                    list = dao.getSchoolInfoList();//获取节点服务器 list
                    nodeServerInfo = list.get(0);//获取节点服务器信息
                    Intent intent = new Intent(this, BackPwdModeActivity.class);
                    //帐号
                    intent.putExtra("account", userAccount);
                    //节点域名
                    intent.putExtra("xtdz", list.get(0).getXtdz());
                    startActivity(intent);
                    AnimUtil.intentSlidIn(this);
                } else {

                    //将用户名传出 跳转到节点列表activity   有多个节点
                    Intent i = new Intent(this, ServiceListActivity.class);
                    i.putExtra("userAccount", userAccount);
                    startActivity(i);
                    AnimUtil.intentSlidIn(this);
                }
                break;
        }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showShortToast(this, "服务器异常");
    }

    //设置点击事件
    @OnClick(R.id.btn_next)
    void setNext() {
        userAccount = account.getText().toString().trim();
        dao.getMySchoolList(userAccount);
    }

    //设置监听事件 输入框都有文字才可跳转
    @OnTextChanged(value = R.id.account, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (StringUtils.isNull(account.getText().toString())) {
            btnNext.setEnabled(false);
        } else {
            btnNext.setEnabled(true);
        }
    }
}


